/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "SyntaxCheckerVisitor.h"
#include "ParameterProcessor.h"
#include <set>
#include <iostream>
#include <fstream>

void SyntaxCheckerVisitor::visit(ASTNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<DefinitionNode*>& node) {
    std::set<std::string> names;
    for (auto& n : node.nodes_) {
        if (!n) {
            continue;
        }
        // 根据不同的子类型获取标识
        std::string name;
        // 使用dynamci_cast测试DefinitionNode的子类型
        if (auto module = dynamic_cast<ModuleDclNode*>(n)) {
            continue;
        } else if (auto constDcl = dynamic_cast<ConstDclNode*>(n)) {
            name = *constDcl->identifier_;
        } else if (auto structDef = dynamic_cast<StructDefNode*>(n)) {
            name = *structDef->identifier_;
        } else if (auto unionDef = dynamic_cast<UnionDefNode*>(n)) {
            name = *unionDef->identifier_;
        } else if (auto enumDcl = dynamic_cast<EnumDclNode*>(n)) {
            name = *enumDcl->identifier_;
        } else if (auto typedefDcl = dynamic_cast<TypeDefDclNode*>(n)) {
            name = *typedefDcl->identifier_;
        } else {
            // 其他类型的定义节点不需要检查
            continue;
        }
        // 检查是否有重名
        if (names.find(name) != names.end()) {
            // 报错并退出
            std::cerr << "Duplicate definition found: " << name << std::endl;
            exit(1);
        } else {
            names.insert(name);
        }
    }
    // 递归访问子节点
    for (auto& n : node.nodes_) {
        if (n) {
            n->accept(*this);
        }
    }
}

void SyntaxCheckerVisitor::visit(SpecificationNode& node) {
    node.definitions_->accept(*this);
}

void SyntaxCheckerVisitor::visit(ModuleDclNode& node) {
    // 递归访问子节点
    if (node.definitions_) {
        node.definitions_->accept(*this);
    }
}

void SyntaxCheckerVisitor::visit(ScopedNameNode& node) {
}

void SyntaxCheckerVisitor::visit(ConstDclNode& node) {
}

void SyntaxCheckerVisitor::visit(ConstTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(ConstExprNode& node) {
}

void SyntaxCheckerVisitor::visit(OrExprNode& node) {
}

void SyntaxCheckerVisitor::visit(XorExprNode& node) {
}

void SyntaxCheckerVisitor::visit(AndExprNode& node) {
}

void SyntaxCheckerVisitor::visit(ShiftExprNode& node) {
}

void SyntaxCheckerVisitor::visit(AddExprNode& node) {
}

void SyntaxCheckerVisitor::visit(MultExprNode& node) {
}

void SyntaxCheckerVisitor::visit(UnaryExprNode& node) {
}

void SyntaxCheckerVisitor::visit(PrimaryExprNode& node) {
}

void SyntaxCheckerVisitor::visit(LiteralNode& node) {
}

void SyntaxCheckerVisitor::visit(PositiveIntConstNode& node) {
}

void SyntaxCheckerVisitor::visit(TypeDclNode& node) {
}

void SyntaxCheckerVisitor::visit(ConstrTypeDclNode& node) {
}

void SyntaxCheckerVisitor::visit(TypeSpecNode& node) {
}

void SyntaxCheckerVisitor::visit(SimpleTypeSpecNode& node) {
}

void SyntaxCheckerVisitor::visit(TemplateTypeSpecNode& node) {
}

void SyntaxCheckerVisitor::visit(BaseTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(SequenceTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(StringTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(WideStringTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(FixedPtTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(MapTypeNode& node) {
}

void SyntaxCheckerVisitor::visit(StructDclNode& node) {
}

void SyntaxCheckerVisitor::visit(StructDefNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<TypeAnnotationNode*>& node) {
    // @FINAL/@APPENDABLE/@MUTABLE三者互斥，最多出现一个
    bool is_final = false;
    bool is_appendable = false;
    bool is_mutable = false;
    for (auto& n : node.nodes_) {
        if (!n) {
            continue;
        }
        TypeAnnotationNode *annotation = n;
        if (annotation->annotation_ == TypeAnnotationNode::AnnotationType::FINAL) {
            is_final = true;
        } 
        if (annotation->annotation_ == TypeAnnotationNode::AnnotationType::APPENDABLE) {
            is_appendable = true;
        }
        if (annotation->annotation_ == TypeAnnotationNode::AnnotationType::MUTABLE) {
            is_mutable = true;
        }
    }
    if (is_final && is_appendable) {
        std::cerr << "FINAL and APPENDABLE annotations are mutually exclusive" << std::endl;
        exit(1);
    }
    if (is_final && is_mutable) {
        std::cerr << "FINAL and MUTABLE annotations are mutually exclusive" << std::endl;
        exit(1);
    }
    if (is_appendable && is_mutable) {
        std::cerr << "APPENDABLE and MUTABLE annotations are mutually exclusive" << std::endl;
        exit(1);
    }
}

void SyntaxCheckerVisitor::visit(TypeAnnotationNode& node) {
}

void SyntaxCheckerVisitor::visit(MemberNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<MemberNode*>& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<FixedArraySizeNode*>& node) {
}

void SyntaxCheckerVisitor::visit(FixedArraySizeNode& node) {
}

void SyntaxCheckerVisitor::visit(MemberAnnotationNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<MemberAnnotationNode*>& node) {
}

void SyntaxCheckerVisitor::visit(StructForwardDclNode& node) {
}

void SyntaxCheckerVisitor::visit(UnionDclNode& node) {
}

void SyntaxCheckerVisitor::visit(UnionDefNode& node) {
}

void SyntaxCheckerVisitor::visit(SwitchTypeSpecNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<CaseNode*>& node) {
}

void SyntaxCheckerVisitor::visit(CaseNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<CaseLabelNode*>& node) {
}

void SyntaxCheckerVisitor::visit(CaseLabelNode& node) {
}

void SyntaxCheckerVisitor::visit(UnionForwardDclNode& node) {
}

void SyntaxCheckerVisitor::visit(EnumDclNode& node) {
}

void SyntaxCheckerVisitor::visit(ListNode<EnumeratorNode*>& node) {
}

void SyntaxCheckerVisitor::visit(EnumeratorNode& node) {
}

void SyntaxCheckerVisitor::visit(TypeDefDclNode& node) {
}

void SyntaxCheckerVisitor::visit(IncludeDclNode& node) {
    // 按照绝对路径检查文件是否存在
    std::string file = *node.file_;
    if (ParameterProcessor::fileExist(file)) {
        // 归一化路径
        ParameterProcessor::normalizePath(file);
        includeFiles_.insert(std::make_pair(&node, file));
        return;
    }
    // 在搜索路径中查找文件
    bool found = false;
    for (auto includePath : this->includePaths_) {
        std::string path = includePath + "/" + file;
        if (ParameterProcessor::fileExist(path)) {
            found = true;
            includeFiles_.insert(std::make_pair(&node, path));
            break;
        }
    }
    if (!found) {
        std::cerr << "Could not find file in include paths: " << file << std::endl;
        exit(1);
    }
}
